<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/14
 * Time: 14:41
 * Created by Created by Panxsoft.
 */

namespace App\Models\Products;

use App\Models\Attributes\Attribute;
use App\Models\Attributes\AttributeDescription;
use App\Models\Concerns\HasCompositePrimaryKey;
use App\Services\Locale\Language;
use App\Services\MobileApp\AppEnv;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * 产品属性文字值（关联属性 / 多语言）
 *
 * @property int language_id
 * @property attribute attribute
 * @property AttributeDescription $attributeDescription
 *
 * @mixin Builder
 */
class ProductAttribute extends Model
{
    use HasCompositePrimaryKey;

    protected $guarded = [];

    protected $table = 'product_attribute';

    protected $primaryKey = ['product_id', 'attribute_id', 'language_id'];

    public $timestamps = false;

    public $incrementing = false;

    protected $casts = [
        'language_id' => 'int',
    ];

    /**
     * @used-by $attributeDescription
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function attributeDescription()
    {
        return $this->belongsTo(AttributeDescription::class, 'attribute_id', 'attribute_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @used-by $attribute
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function attribute()
    {
        return $this->belongsTo(Attribute::class, 'attribute_id', 'attribute_id');
    }
}